package Funcionalidades.Biometria;

import br.com.beautycenter.DAO.PersisteDigital;
import br.com.beautycenter.bean.Cliente;
import br.com.beautycenter.bean.Digital;
import com.digitalpersona.onetouch.*;
import com.digitalpersona.onetouch.ui.swing.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 * Enrollment control test 
 */
public class CadastrarDigital extends JDialog {
    private EnumMap<DPFPFingerIndex, DPFPTemplate> templates;
    PersisteDigital persisteDigital = new PersisteDigital();
    
    Cliente cliente;
    
    JButton confirmar = new JButton("Confirmar e Fechar");
    
    
    public void limparCadastroDigitais(java.util.List<Digital> lista){
        for(Digital item: lista){
            persisteDigital.deletar(persisteDigital.retornarDigitalPeloId(item));
        }
    }
    
    /**
     * Remove todas as digitais cadastras e salvar as que estão no template
     */
    public void cadastrarDigitais(){
        try{
            limparCadastroDigitais(cliente.getListaDigitais());
        } catch (Exception ex){
            JOptionPane.showMessageDialog(null, "Não foi possível remover as digitais antigas. Tente novamente mais tarde.");
            ex.printStackTrace();
            return;
        }
        
        for(Map.Entry<DPFPFingerIndex, DPFPTemplate> mapa: templates.entrySet()){
            Digital temp = new Digital();
            temp.setIdDigital(0);
            temp.setCliente(cliente);
            temp.setDedo(mapa.getKey().name());
            temp.setDigital(mapa.getValue().serialize());
            
            persisteDigital.salvar(temp);
        }
    }
    
    public CadastrarDigital(Frame owner, int maxCount, final String reasonToFail, EnumMap<DPFPFingerIndex, DPFPTemplate> templates, Cliente cliente) {
        super (owner, true);
        this.templates = templates;
        this.cliente = cliente;
        
        ativarBotao();

        setTitle("Cadastrar Digital");

        DPFPEnrollmentControl enrollmentControl = new DPFPEnrollmentControl();

        EnumSet<DPFPFingerIndex> fingers = EnumSet.noneOf(DPFPFingerIndex.class);
        fingers.addAll(templates.keySet());
        enrollmentControl.setEnrolledFingers(fingers);
        enrollmentControl.setMaxEnrollFingerCount(maxCount);

        enrollmentControl.addEnrollmentListener(new DPFPEnrollmentListener() {
            public void fingerDeleted(DPFPEnrollmentEvent e) throws DPFPEnrollmentVetoException {
                if (reasonToFail != null) {
                    throw new DPFPEnrollmentVetoException(reasonToFail);
                } else {
                    CadastrarDigital.this.templates.remove(e.getFingerIndex());
                }
                
                ativarBotao();
            }
            
            public void fingerEnrolled(DPFPEnrollmentEvent e) throws DPFPEnrollmentVetoException {
                if (reasonToFail != null) {
                    //e.setStopCapture(false);
                    throw new DPFPEnrollmentVetoException(reasonToFail);
                } else{
                    CadastrarDigital.this.templates.put(e.getFingerIndex(), e.getTemplate());
                }
                
                ativarBotao();
            }
        });
        
        getContentPane().setLayout(new BorderLayout());
        
        JButton closeButton = new JButton("Cancelar");
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);                //End Dialog
            }
        });
        
        confirmar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cadastrarDigitais();
                dispose();
            }
        });
        
        JPanel bottom = new JPanel();
        bottom.add(confirmar);
        bottom.add(closeButton);
        add(enrollmentControl, BorderLayout.CENTER);
        add(bottom, BorderLayout.PAGE_END);
        
        pack();
        setLocationRelativeTo(null);
        
    }
    
    public void ativarBotao(){
        if(templates.isEmpty()){
            confirmar.setEnabled(false);
        }else{
            confirmar.setEnabled(true);
        }
    }
}